package com.yiren.dbaa.view.adapter.line;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.yiren.backstage.currency.entity.ProjectEntity;
import com.yiren.backstage.currency.utils.StringUtil;
import com.yiren.dbaa.R;

import java.util.List;

/**
 * @author lwb
 * @date : 2022/3/17 14:50
 * @description: 线路项目适配器
 */
public class PointNameListAdapter extends BaseAdapter {

    private Context context;
    private List<ProjectEntity> projectList;   //项目list
    private OnProjectActionListener listener;

    public PointNameListAdapter(Context context, List<ProjectEntity> projectList, OnProjectActionListener listener) {
        this.context = context;
        this.projectList = projectList;
        this.listener = listener;
    }

    @Override
    public int getCount() {
        return projectList.size();
    }

    @Override
    public Object getItem(int arg0) {
        return projectList.get(arg0);
    }

    @Override
    public long getItemId(int arg0) {
        return arg0;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup arg2) {
        PointNameListAdapter.ViewHodler hodler = null;
        if (null == convertView) {
            hodler = new PointNameListAdapter.ViewHodler();
            convertView = (LinearLayout) LayoutInflater.from(context).inflate(R.layout.item_project_template, null);
            hodler.btnProjectIcon = (Button) convertView.findViewById(R.id.btn_project_icon);
            hodler.tvProjectName = (TextView) convertView.findViewById(R.id.tv_project_name);
            convertView.setTag(hodler);
        } else {
            hodler = (PointNameListAdapter.ViewHodler) convertView.getTag();
        }
        hodler.btnProjectIcon.setText("");
        final ProjectEntity projectInfo = (ProjectEntity) projectList.get(position);
        if (projectInfo.getImageUri().contains("default_")) {
            // 如果图片地址是默认图片，那么按钮就上文字
            if (!StringUtil.isEmpty(projectInfo.getAbbreviation())) {
                hodler.btnProjectIcon.setText(projectInfo.getAbbreviation());
            } else {
                if (projectInfo.getName().length() > 2) {
                    hodler.btnProjectIcon.setText(projectInfo.getName().substring(0, 2));
                } else {
                    hodler.btnProjectIcon.setText(projectInfo.getName());
                }
            }
        }
        int resId = context.getResources().getIdentifier("hjl_" + projectInfo.getImageUri(), "mipmap", context.getPackageName());
        hodler.btnProjectIcon.setBackgroundResource(resId);
        hodler.tvProjectName.setText(projectInfo.getName());
        convertView.setOnClickListener(v -> {
            listener.onProjectAction(position);
        });
        return convertView;
    }

    class ViewHodler {
        Button btnProjectIcon;
        TextView tvProjectName;
    }


    public interface OnProjectActionListener {
        void onProjectAction(int position);
    }

}
